<template>
  <div class="login-container" style="z-index: 9">
    <!-- 注册卡片 -->
    <transition name="fade-up">
      <el-card class="login-box">
        <h1 class="title">用户注册</h1>
        <el-divider />

        <el-form
          ref="registerForm"
          style="max-width: 600px"
          :model="registerForm"
          :rules="rules"
          label-width="auto"
          :size="formSize"
        >
          <!-- 用户名输入 -->
          <el-form-item prop="username">
            <el-input
              v-model="registerForm.username"
              prefix-icon="el-icon-user"
              placeholder="用户名"
              size="large"
              clearable
            />
          </el-form-item>
          <!-- 用户类型选择 -->
          <el-form-item prop="userType">
            <el-select
              v-model="registerForm.userType"
              placeholder="请选择用户类型"
              style="width: 100%"
            >
              <el-option label="用户" value="user"></el-option>
              <el-option label="供应商" value="supplier"></el-option>
            </el-select>
          </el-form-item>

          <!-- 密码输入 -->
          <el-form-item prop="password">
            <el-input
              v-model="registerForm.password"
              prefix-icon="el-icon-lock"
              type="password"
              placeholder="密码"
              size="large"
              show-password
            />
          </el-form-item>

          <!-- 确认密码输入 -->
          <el-form-item prop="confirmPassword">
            <el-input
              v-model="registerForm.confirmPassword"
              prefix-icon="el-icon-lock"
              type="password"
              placeholder="确认密码"
              size="large"
              show-password
            />
          </el-form-item>
          <el-form-item prop="phone">
            <el-input
              v-model="registerForm.phone"
              prefix-icon="el-icon-phone"
              placeholder="联系电话"
              size="large"
            />
          </el-form-item>

          <el-form-item prop="address">
            <el-input
              v-model="registerForm.address"
              prefix-icon="el-icon-location"
              placeholder="联系地址"
              size="large"
            />
          </el-form-item>
          <!-- 注册按钮 -->
          <el-button
            type="primary"
            size="large"
            class="login-btn"
            @click="handleRegister('registerForm')"
          >
            注册
          </el-button>
        </el-form>
        <el-link type="primary" @click="goToLogin">已有账号? 登录</el-link>
      </el-card>
    </transition>
  </div>
</template>

<script>
import { mapActions } from "vuex"; // 引入 mapActions

export default {
  data() {
    return {
      registerForm: {
        username: "",
        password: "",
        confirmPassword: "",
        userType: "user", // 默认用户类型
        phone: "",
        address: "",
      },
      formSize: "default",
    };
  },
  methods: {
    ...mapActions("user", ["addUser"]), // 确保模块名与命名空间匹配
    ...mapActions("supplier", ["addSupplier"]),
    async handleRegister(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          if (
            this.registerForm.password !== this.registerForm.confirmPassword
          ) {
            return this.$message.error("两次密码不一致");
          }

          const newUser = {
            id:
              (this.registerForm.userType == "user" ? "U" : "S") +
              Date.now().toString().slice(-6), // 生成更规范的用户ID
            username: this.registerForm.username,
            password: this.registerForm.password,
            phone: this.registerForm.phone, // 使用表单实际值
            address: this.registerForm.address,
            customer_type: this.registerForm.userType,
          };

          try {
            if (this.registerForm.userType === "user") {
              await this.addUser(newUser);
            } else {
              await this.addSupplier(newUser);
            }
            this.$message.success("注册成功");
            this.$router.push("/login");
          } catch (error) {
            this.$message.error("注册失败: " + error.message);
          }
        }
      });
    },
    goToLogin() {
      this.$router.push("/login");
    },
  },
  computed: {
    rules() {
      return {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        confirmPassword: [
          { required: true, message: "请确认密码", trigger: "blur" },
          { validator: this.validateConfirmPassword, trigger: "blur" },
        ],
        phone: [{ required: true, message: "请输入联系电话", trigger: "blur" }],
        address: [
          { required: true, message: "请输入联系地址", trigger: "blur" },
        ],
      };
    },
  },
};
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
  background-image: url("@/assets/images/register-bg.png");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}
.login-box {
  width: 400px;
  padding: 40px 30px;
  border-radius: 20px;
  z-index: 2;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(2px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  background-color: rgba(255, 255, 255, 0.6);
  animation: cardEnter 0.6s cubic-bezier(0.25, 0.46, 0.45, 0.94) both;
  transform-origin: center;
}
@keyframes cardEnter {
  0% {
    opacity: 0;
    transform: translateY(20px) scale(0.95);
  }
  100% {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}
.title {
  text-align: center;
  color: #2c3e50;
  margin: 0 0 20px;
  font-size: 28px;
  font-weight: 600;
}
.login-btn {
  width: 100%;
  margin-top: 10px;
  font-size: 16px;
  letter-spacing: 2px;
  background: linear-gradient(135deg, #409eff 0%, #36a2ff 100%);
  border: none;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}
.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(64, 158, 255, 0.3);
}
@media (max-width: 768px) {
  .login-box {
    width: 90%;
    padding: 30px 20px;
  }
}
</style>
